package com.way.fileexplorer;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;

import com.way.fileexplorer.util.Slog;
import com.way.fileexplorer.util.StorageUtil;

public class StorageReceiver extends BroadcastReceiver {

	private static final String TAG = "FileExplorerReceiver";

	@Override
	public void onReceive(Context context, Intent intent) {
		Slog.d(TAG, "Receive Broadcast, action=" + intent.getAction());
		if (Intent.ACTION_MEDIA_MOUNTED.equals(intent.getAction())
				|| Intent.ACTION_MEDIA_EJECT.equals(intent.getAction())
				|| Intent.ACTION_MEDIA_BAD_REMOVAL.equals(intent.getAction())
				|| Intent.ACTION_MEDIA_UNMOUNTED.equals(intent.getAction())) {
			Handler handler = new Handler(context.getMainLooper());
			if(handler != null){
				handler.postDelayed(new CheckStorageRunable(), 1000);
			}
		}
	}

	private class CheckStorageRunable implements Runnable {
		@Override
		public void run() {
			if (StorageUtil.isEnableNotify()) {
				StorageUtil.notifyStorageChanged();
			}
		}
	}
}